/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package phongmodel;

import java.awt.Color;

/**
 *
 * @author Apapus
 */
public class MyPhong {
    
private static final double Ii = 0.5;

    public static double PhongCalculate(MySphere sphere, Light light, Vector3D vecN, Vector3D vecL, Vector3D vecR, Vector3D vecV, double fatt){
        double result = 0.0;
        
        result = (sphere.getKa() * light.getIA() + light.getIP()*(sphere.getKd()* Math.max(0,Vector3D.dot(vecN, vecL)) + sphere.getKs() * Ii *Math.pow(Math.max(0,(Vector3D.dot(vecR, vecV))),sphere.getN())) );
     
        return result;
    }
    
    public static Color calculateColor(MySphere sphere, Light light, Vector3D vecN, Vector3D vecL, Vector3D vecR, Vector3D vecV, double fatt){
        Color result = null;
        
        double r = calculateComponent(sphere, light, vecN, vecL, vecR, vecV, fatt, sphere.getKolor().getRed()/255, sphere.getKolor().getRed()/255, light.getColor().getRed()/255);
        double g = calculateComponent(sphere, light, vecN, vecL, vecR, vecV, fatt, sphere.getKolor().getGreen()/255, sphere.getKolor().getGreen()/255, light.getColor().getGreen()/255);
        double b = calculateComponent(sphere, light, vecN, vecL, vecR, vecV, fatt, sphere.getKolor().getBlue()/255, sphere.getKolor().getBlue()/255, light.getColor().getBlue()/255);
        
        r = Renderer.norm0to1(r);
        g = Renderer.norm0to1(g);
        b = Renderer.norm0to1(b);
        
        r *= 255;
        g *= 255;
        b *= 255;
        
        //System.out.println(r + " " + g + " " +b);
        
        result = new Color((int)r, (int)g, (int)b);
        return result;
    }
    
    public static double calculateComponent(MySphere sphere, Light light, Vector3D vecN, Vector3D vecL, Vector3D vecR, Vector3D vecV, double fatt, double ambientComponent, double diffuseComponent, double specularComponent){
        double result = 0.0;
        
        result = (sphere.getKa() * light.getIA() * ambientComponent +  light.getIP()*(diffuseComponent * sphere.getKd()* Math.max(0,Vector3D.dot(vecN, vecL)) + specularComponent * sphere.getKs() * Ii *Math.pow(Math.max(0,(Vector3D.dot(vecR, vecV))),sphere.getN())) );        
     
        return result;
    }
}
